import CommonMenu from '@/layouts/commonMenu.json'
import { getFullUrl } from '@/utils'
import DpaMenu from '@/layouts/dpaMenu.json'
import LayoutConfig from '@/layouts/layoutConfig.json'

export class LayoutMenu {
  codePathMap: Map<string, Object>
  pathCodeMap: Map<string, string>
  menuExpandKeys: Array<string>
  menuInfo: Array<any>
  constructor() {
    this.codePathMap = new Map()
    this.pathCodeMap = new Map()
    this.menuExpandKeys = []
    let _menuInfo = []
    if (LayoutConfig.layoutPlan === 'DPA_NAVIGATION') {
      ;['leftMenuList', 'rightMenuList', 'bottomMenuList'].forEach(key => {
        if (DpaMenu[key]) {
          _menuInfo.push(...DpaMenu[key])
        }
      })
    } else {
      _menuInfo = CommonMenu
    }
    this.menuInfo = _menuInfo
      .filter(item => item !== null && item !== undefined)
      .flatMap(item => item)
      .map(item => this.buildMenuInfo(item))
      .filter(item => item !== null && !item.hideMenu)
  }

  buildMenuInfo(router) {
    let url = router.menuUrl
    if (
      router?.contentSetting?.settingType &&
      'ZERO_CODE_VISUALIZATION_LARGE_SCREEN' === router?.contentSetting?.settingType
    ) {
      url = url.replace('allin-app/adv', 'dashboard')
    }
    const info = {
      name: router?.code ? router.code : '',
      title: router?.title,
      code: router?.code ? router.code : '',
      routeName: this.getMenuName(router),
      icon: router?.icon,
      selectedIcon: router?.selectedIcon,
      children: [] as any[],
      path: decodeURIComponent(getFullUrl(url)),
      iconDisplay: router?.iconDisplay,
      parentId: router.parentId,
      hideMenu: ['CUSTOM_PAGE', 'PRO_CODE_PAGE'].includes(router?.settingType) ? !router.menuShow : false // 定制化页面和自定义页面可配置显示菜单
    }
    if (router.menuExpandFlag && info.name && !this.menuExpandKeys.includes(info.name)) {
      this.menuExpandKeys.push(info.name)
    }
    // 不隐藏子菜单
    const children = router.children
      ?.map(router => this.buildMenuInfo(router))
      .filter(item => item !== null && !item.hideMenu)
    if (children && children.length > 0) {
      info.children = info.children.concat(children)
    } else {
      this.codePathMap.set(info.name, info)
      this.pathCodeMap.set(info.path, info.name)
    }
    return info
  }

  getMenuName(menuItem) {
    const { contentSetting, menuId } = menuItem
    const MENU_NAME_MAP = {
      FORM_LIST: 'menu_form_',
      TABLE_LIST: 'menu_table_',
      PAGE_LIST: 'menu_page_',
      SYSTEM_MENU: 'menu_system_',
      BI_DASH_BOARD: 'menu_bi_publish_',
      VISUALIZATION_LARGE_SCREEN: 'menu_visualization_publish_',
      ALLIN_RESOURCE_MENU: 'menu_resource_',
      CUSTOM_PAGE: 'menu_custom_page_'
    }

    if (!contentSetting.settingId) {
      return 'menu_page_' + menuId
    }
    if (contentSetting.settingType === 'CUSTOM_PAGE') {
      return MENU_NAME_MAP[contentSetting.settingType] + menuId
    } else {
      return MENU_NAME_MAP[contentSetting.settingType] + contentSetting.settingId
    }
  }

  public getPathByCode(path: string) {
    return this.pathCodeMap.get(path)
  }
}
